package com.health.platform.common.sys.organization.service;

import com.health.platform.common.restful.AppResponse;
import com.health.platform.common.sys.login.entity.UserEntity;
import com.health.platform.common.sys.organization.dao.OrganizationMapper;
import com.health.platform.common.sys.organization.entity.OrganizationEntity;
import com.health.platform.common.sys.organization.entity.UnitEntity;
import com.health.platform.common.utils.ThreadLocalUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>区划Service</p>
 * <p>创建日期：2021-02-20</p>
 *
 * @author zhou-qf
 */
@Service
public class OrganizationService {


    @Resource
    private OrganizationMapper organizationMapper;

    /**
     * 通过条件查询区划信息
     * @param organizationEntity 查询条件
     * @return 结果
     */
    public AppResponse getOrganization(OrganizationEntity organizationEntity){
        // 如果查询当前登录用户的区划信息
        if(organizationEntity.getCurrentQuery() == 1){
            UserEntity userEntity = ThreadLocalUtil.get();
            organizationEntity.setSgbCode(userEntity.getBody().getAreainfo().getCode());
        }
        return AppResponse.success("查询成功", organizationMapper.getOrganization(organizationEntity));
    }

    /**
     * 查询机构等级 目前是固定的只有3个级别
     * @return 结果
     */
    public AppResponse getUnitLevel(){
        List<Map> list = new ArrayList<>();
        Map<String, String> map1 = new HashMap(){
            {
                put("key", "1");
                put("value", "一级机构");
            }
        };
        Map<String, String> map2 = new HashMap(){
            {
                put("key", "2");
                put("value", "二级机构");
            }
        };
        Map<String, String> map3 = new HashMap(){
            {
                put("key", "3");
                put("value", "三级机构");
            }
        };
        list.add(map1);
        list.add(map2);
        list.add(map3);
        return AppResponse.success("查询成功", list);
    }

    /**
     * 通过条件查询机构信息
     * @param unitEntity 查询条件
     * @return 结果
     */
    public AppResponse getUnit(UnitEntity unitEntity){
        return AppResponse.success("查询成功", organizationMapper.getUnit(unitEntity));
    }
}
